﻿using System;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;
using ExtendLib.Helper;
using Ivony.Fluent;
using Ivony.Html;
using Ivony.Html.Parser;

namespace SmsBomb
{
    public partial class ProxyParse : Form
    {
        private int? _keyId { get; set; }
        public ProxyParse()
        {
            InitializeComponent();
        }
        #region 事件
        /// <summary>
        ///窗口加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ProxyParse_Load(object sender, EventArgs e)
        {
            DataBind();
        }

        /// <summary>
        /// 数据保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            string url = this.txtUrl.Text,
                rule=txtRule.Text,
                status = this.cbStatus.SelectedItem.ToString();
            if (url.IsNullOrEmpty() || rule.IsNullOrEmpty() || status.IsNullOrEmpty())
                return;
            var model = new ProxyParseModel
            {
                Id = _keyId,
                SelectRule = rule,
                WebUrl = url,
                Status = status
            };
            bool r = ProxyParseHelper.Create(model);
            if (r)
            {
                DataBind();
                _keyId =null;
                this.labMsg.Text = "";

                this.txtUrl.Text = txtRule.Text="";

                return;
            }
            MessageBox.Show("操作失败");
        }

        /// <summary>
        /// 编辑选中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAction_Click(object sender, EventArgs e)
        {
            var keyId = int.Parse(dgvList.SelectedRows[0].Cells[0].Value.ToString());
            var model = ProxyParseHelper.GetItem(keyId);
            this._keyId = model.Id;
            this.txtUrl.Text = model.WebUrl;
            this.txtRule.Text = model.SelectRule;
            this.cbStatus.SelectedItem = model.Status;

            this.labMsg.Text = "正在编辑主键："+_keyId;
        }
        /// <summary>
        /// 单元格点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var keyId = int.Parse(dgvList.SelectedRows[0].Cells[0].Value.ToString());
            var model = ProxyParseHelper.GetItem(keyId);

            this.txtCheckUrl.Text = model.WebUrl;
            this.txtCheckRule.Text = model.SelectRule;
        }
        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCheck_Click(object sender, EventArgs e)
        {
            this.txtCheckResult.Text = "";

           // WebClient wc=new WebClient();
            //wc.Headers.Add("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
            // var contentData=wc.DownloadData(this.txtCheckUrl.Text);
           // string content =Encoding.UTF8.GetString(contentData);
            var hri=new HttpRequestItem();
            hri.Url = this.txtCheckUrl.Text;
            var tempHtml = HttpHelper.GetHtml(hri);
            if (tempHtml.DataInfo.IsNullOrEmpty())
                return;
            var docment = new JumonyParser().Parse(tempHtml.DataInfo);
            var list = docment.Find(this.txtCheckRule.Text);
            foreach (var item in list)
            {
                this.txtCheckResult.Text += item.InnerText().Trim() + ":" + item.NextElement().InnerText().Trim() + "\r\n";
            }
        }
        /// <summary>
        /// 导出所有
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExport_Click(object sender, EventArgs e)
        {
            string temp = this.txtCheckResult.Text;
            if(temp.Trim().Length<=0)
                return;
            //定义一个文件打开控件
            SaveFileDialog ofd = new SaveFileDialog();
            //设置打开对话框的初始目录，默认目录为exe运行文件所在的路径
            ofd.InitialDirectory = Application.StartupPath;
            //设置打开对话框的标题
            ofd.Title = "请选择要打开的文件";
            //设置对话框是否记忆之前打开的目录
            ofd.RestoreDirectory = true;
            ofd.Filter = "文本文件|*.txt";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                //获取用户选择的文件完整路径
                string filePath = ofd.FileName;
                File.WriteAllText(filePath, temp);
            }
            this.txtCheckResult.Text = "";
        }
        #endregion

        #region 自定义方法
        /// <summary>
        /// 数据绑定
        /// </summary>
        private void DataBind()
        {
            this.dgvList.DataSource = ProxyParseHelper.GetAll();
        } 
        #endregion

       

        
      

       
    }
}
